package com.open.capacity.controller;

import com.open.capacity.common.web.Result;
import com.open.capacity.model.LoginModel;
import com.open.capacity.model.UserModel;
import com.open.capacity.service.LoginService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author: y_022
 * @date: 11:35-2021/6/26
 * @description:个人中心登录
 */
@Slf4j
@Api(tags = "LOGIN API")
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * 用户登录，使用手机号或者邮箱
     *
     * @param loginModel
     * @param httpSession
     * @return
     */
    @PostMapping("/loginCheck")
    public Result Login(LoginModel loginModel, HttpSession httpSession) {
        if(loginModel.getUserna().contains("@")){
            loginModel.setEmails(loginModel.getUserna());
        }else{
            loginModel.setPhones(loginModel.getUserna());
        }
        String locode = loginModel.getLocode();
        if (!locode.equals(httpSession.getAttribute("code"))) {
            return Result.failed("验证码不正确!");
        }
        //todo 登录校验成功之后，不再做其他操作，只是简单返回登录的user对象，或有其他操作可后期增加
        try {
            //通过用户名或手机号查询是否存在相应用户
            UserModel userModel = loginService.login(loginModel);
            //进行校验操作，校验密码和状态
            if (userModel.getEnable() == 0) {
                return Result.failed("该用户已被禁用！");
            }
            //明文密码使用MD5加密后操作
            String passwd = DigestUtils.md5DigestAsHex(loginModel.getPasswd().getBytes());
            return passwd.equals(userModel.getPasswd()) ? Result.succeed(userModel, "操作成功！") : Result.failed("密码错误！");
        } catch (Exception e) {
            log.error("登录操作失败，请查看异常描述！", e);
        }
        return Result.failed("用户不存在或被禁用！");
    }

    /**
     * 用户注册
     *
     * @param userModel
     * @return
     */
    @PostMapping("/register")
    public Result Register(UserModel userModel) {
        //首先校验号码和手机号是否已被注册！
        UserModel user = null;
        UserModel user1 = null;
        try {
             user = loginService.findUserByPhones(userModel.getPhones());
            if (user != null) {
                return Result.failed("号码已被注册！");
            }
             user1 = loginService.findUserByEmails(userModel.getEmails());
            if (user1 != null) {
                return Result.failed("邮箱已被注册！");
            }
            loginService.register(userModel);
        }catch (Exception e){
            log.error("error!",e);
        }
        user = loginService.findUserByEmails(userModel.getEmails());
        return Result.succeed(user,"注册成功！");
    }
}